package com.hafu.swing;

import com.hafu.model.User;
import com.hafu.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 登录界面框架
public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private UserService userService = new UserService();

    public LoginFrame() {
        initUI();
    }

    private void initUI() {
        setTitle("网上教育系统 - 登录");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        setLocationRelativeTo(null);
        setResizable(false);

        // 顶部标题
        JPanel topPanel = new JPanel();
        topPanel.setBackground(new Color(51, 153, 255));
        JLabel titleLabel = new JLabel("网上教育系统", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setPreferredSize(new Dimension(getWidth(), 50));
        topPanel.add(titleLabel);
        add(topPanel, BorderLayout.NORTH);

        // 中部登录表单
        JPanel formPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        usernameField = new JTextField(15);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField = new JPasswordField(15);
        passwordField.setEchoChar('*');

        formPanel.add(usernameLabel);
        formPanel.add(usernameField);
        formPanel.add(passwordLabel);
        formPanel.add(passwordField);

        add(formPanel, BorderLayout.CENTER);

        // 底部按钮
        JPanel buttonPanel = new JPanel();
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        loginButton.setPreferredSize(new Dimension(100, 35));
        loginButton.setBackground(new Color(51, 153, 255));
        loginButton.setForeground(Color.WHITE);
        loginButton.setBorderPainted(false);
        loginButton.setFocusPainted(false);

        JButton resetButton = new JButton("重置");
        resetButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        resetButton.setPreferredSize(new Dimension(100, 35));
        resetButton.setBackground(new Color(200, 200, 200));
        resetButton.setBorderPainted(false);
        resetButton.setFocusPainted(false);

        // 登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();

                // 输入验证
                if (username.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "用户名和密码不能为空", "输入错误", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 调用服务验证用户
                User user = userService.login(username, password);
                if (user != null) {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "登录成功！欢迎 " + user.getUsername(),
                            "登录成功", JOptionPane.INFORMATION_MESSAGE);

                    // 登录成功后打开主界面并传递用户信息
                    SwingUtilities.invokeLater(() -> {
                        new MainFrame(user.getUserId(), user.getRole(), user.getUsername());
                    });

                    // 关闭登录窗口
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "登录失败！用户名或密码错误",
                            "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 重置按钮事件
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                usernameField.setText("");
                passwordField.setText("");
                usernameField.requestFocus();
            }
        });

        buttonPanel.add(loginButton);
        buttonPanel.add(resetButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginFrame().setVisible(true);
        });
    }
}